<?php
/**
 * Created by PhpStorm.
 * User: adminchyistrator
 * Date: 2018/12/18
 * Time: 18:42
 */

namespace app\adminchy\controller;

use think\facade\Request;
use think\facade\Session;
use app\adminchy\controller\Base;

class reward extends Base
{
    //扫雷房间奖励列表
    public function sweepRewardList(){
//        $search = Request::param('search');
        $sweepId = db('room')->where('type',1)->column('room_id');
        $sql = db('prize_rule')->whereIn('room_id',$sweepId);
        $sweepRewardList=$sql->paginate(15)->each(function ($item,$key){
            $item['type'] = $item['type'] == 1 ? '发包者奖励' :'抢包者奖励';
            $item['room_name'] = db('room')->where(['room_id'=>$item['room_id']])->value('room_name');
            return $item;
        });
//        $this->assign('search',$search);
        $this->assign('sweepRewardList',$sweepRewardList);
        return view('Reward/sweepRewardList');
    }
    //扫雷奖励信息
    public function sweepPrizeInfo(){
        $id = Request::param('prize_id');
        $prizeInfo = db('prize_rule')->where('prize_id',$id)->find();
        $prizeInfo['room_name'] = db('room')->where(['room_id'=>$prizeInfo['room_id']])->value('room_name');
        $this->assign('prizeInfo', $prizeInfo);
        return view('Reward/sweepPrizeInfo');
    }
    //修改扫雷奖励
    public function editSweepPrize(){
        $data = Request::post();
        if ($data['prize_money']<0.01){
            return json(['status'=>1,'message'=>'奖励金额错误']);
        }
        $datas=[
            'snatch_money' =>trim($data['snatch_money']),
            'prize_money'=>trim($data['prize_money']),
            'type'=>$data['type'],
            'update_time'=>date('Y-m-d H:i:s',time())
        ];
        $res = db('prize_rule')->where(['prize_id'=>$data['prize_id']])->update($datas);
        if ($res){
            return json(['status'=>0,'message'=>'修改奖励设置成功']);
        }else{
            return json(['status'=>1,'message'=>'修改奖励设置失败']);
        }
    }
    //接龙房间奖励列表
    public function solitaireRewardList(){
//        $search = Request::param('search');
        $sweepId = db('room')->where('type',2)->column('room_id');
        $sql = db('prize_rule')->whereIn('room_id',$sweepId);
        $solitaireRewardList=$sql->paginate(15)->each(function ($item,$key){
            $item['type'] = $item['type'] == 1 ? '发包者奖励' :'抢包者奖励';
            $item['room_name'] = db('room')->where(['room_id'=>$item['room_id']])->value('room_name');
            return $item;
        });
//        $this->assign('search',$search);
        $this->assign('solitaireRewardList',$solitaireRewardList);
        return view('Reward/solitaireRewardList');
    }
    //接龙奖励信息
    public function solitairePrizeInfo(){
        $id = Request::param('prize_id');
        $prizeInfo = db('prize_rule')->where('prize_id',$id)->find();
        $prizeInfo['room_name'] = db('room')->where(['room_id'=>$prizeInfo['room_id']])->value('room_name');
        $this->assign('prizeInfo', $prizeInfo);
        return view('Reward/solitairePrizeInfo');
    }
    //修改接龙奖励
    public function editSolitairePrize(){
        $data = Request::post();
        if ($data['prize_money']<0.01){
            return json(['status'=>1,'message'=>'奖励金额错误']);
        }
        $datas=[
            'snatch_money' =>trim($data['snatch_money']),
            'prize_money'=>trim($data['prize_money']),
            'type'=>$data['type'],
            'update_time'=>date('Y-m-d H:i:s',time())
        ];
        $res = db('prize_rule')->where(['prize_id'=>$data['prize_id']])->update($datas);
        if ($res){
            return json(['status'=>0,'message'=>'修改奖励设置成功']);
        }else{
            return json(['status'=>1,'message'=>'修改奖励设置失败']);
        }
    }
    //删除奖励设置
    public function delete_prize(){
        $prize_id = Request::param('prize_id');
        $res = db('prize_rule')->where(['prize_id'=>$prize_id])->find();
        if (!$res){
            return json(['status'=>1,'message'=>'该奖励设置不存在']);
        }
        $delPrice = db('prize_rule')->where(['prize_id'=>$prize_id])->delete();
        if ($delPrice){
            return json(['status'=>0,'message'=>'删除奖励设置成功']);
        }else{
            return json(['status'=>1,'message'=>'删除奖励设置失败']);
        }
    }
}